package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.service.RightService;
import com.ciei.dpagm.service.RoleService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private RightService rightService;

	@RequestMapping("role/loadRolePage")
	@ResponseBody
	public JSONObject loadRolePage(@RequestBody JSONObject page) {
		return roleService.loadRolePage(getPageParams(page));
	}

	@RequestMapping("role/loadRoleGrid")
	@ResponseBody
	public JSONObject loadRoleGrid(@RequestBody JSONObject page) {
		return roleService.loadRoleGrid(getPageParams(page));
	}

	@RequestMapping("role/add")
    public JSONObject add(@RequestBody JSONObject roleJson) {
        return roleService.saveOrUpdate(roleJson);
    }

	@RequestMapping("role/update")
	public JSONObject update(@RequestBody JSONObject roleJson) {
		return roleService.saveOrUpdate(roleJson);
	}

	@RequestMapping("role/delete")
	public JSONObject deleteRole(Integer roleId) {
		return roleService.deleteRole(roleId);
	}

	@RequestMapping(value = "role/findById")
	public JSONObject findById(Integer roleId) {
		return roleService.findDataById(roleId);
	}

	@RequestMapping(value = "vue/role/loadRoleComboOption")
	public JSONObject loadRoleComboOption(@RequestParam(required = false) List<Integer> ignoreIds) {
		Users user = (Users)SecurityUtils.getSubject().getPrincipal();
		return roleService.loadRoleComboOption(user, ignoreIds);
	}

	@RequestMapping("role/enableRole")
	@ResponseBody
	public JSONObject enableRole(Integer roleId, Integer enable) {
		return roleService.enableRole(roleId, enable);
	}

	@RequestMapping(value = "role/loadRightTree")
	@ResponseBody
	public JSONObject loadRightTree() {
		return JsonUtil.getSuccess("").fluentPut(APP.DATA, rightService.loadTree(true, false));
	}

	@RequestMapping("role/setRoleRights")
	@ResponseBody
	public JSONObject setRoleRights(@RequestBody JSONObject configJson) {
		return roleService.setRoleRights(configJson);
	}

	@RequestMapping("role/addRoleUsers")
	@ResponseBody
	public JSONObject addRoleUsers(@RequestBody JSONObject configJson) {
		return roleService.addRoleUsers(configJson);
	}

	@RequestMapping("role/removeRoleUsers")
	@ResponseBody
	public JSONObject removeRoleUsers(@RequestBody JSONObject configJson) {
		return roleService.removeRoleUsers(configJson);
	}

	@RequestMapping("role/loadUserGridInRole")
	@ResponseBody
	public JSONObject loadUserGridInRole(@RequestBody JSONObject page) {
		return roleService.loadUserGridInRole(getPageParams(page));
	}

	@RequestMapping("role/loadUserGridOutRole")
	@ResponseBody
	public JSONObject loadUserGridOutRole(@RequestBody JSONObject page) {
		return roleService.loadUserGridOutRole(getPageParams(page));
	}
}
